package com.jy.rhin.application.infra.assembler;

import com.alibaba.fastjson.JSONObject;
import com.jy.rhin.domain.notification.publish.entity.PublishMessageDO;
import com.jy.rhin.sdk.dto.notification.resp.PublishMessageDto;
import com.jy.rhin.sdk.dto.notification.resp.SubscribeAndFilterDto;
import org.springframework.beans.BeanUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author dhl
 * @date 2022/7/7
 * @Description
 */
public class PublishMessageAssembler {

    private PublishMessageAssembler(){}

    /**
     * 封装返回bean
     * @param records
     * @param subscribeAndFilterMap
     * @param messageContent
     * @return
     */
    public static List<PublishMessageDto> toDto(List<PublishMessageDO> records, Map<String, SubscribeAndFilterDto> subscribeAndFilterMap, JSONObject messageContent, String messageId){
        //do转dto 方便聚合服务直接使用
        return records.stream().map(entry -> {
            PublishMessageDto dto = new PublishMessageDto();
            BeanUtils.copyProperties(entry, dto);
            dto.setTraceId(messageId);
            dto.setMessage(messageContent.toJSONString());
            dto.setNotifyStatus(entry.getNotifyStatus().name());
            SubscribeAndFilterDto filterDto = subscribeAndFilterMap.get(entry.getSubscriberCode());
            dto.setTopicExp(filterDto.getTopicExp());
            dto.setAddressUrl(filterDto.getAddressUrl());
            dto.setSubscribeUid(filterDto.getSubscribeUid());
            dto.setInteractionType(filterDto.getInteractionType());
            return dto;
        }).collect(Collectors.toList());
    }
}
